package tjs.study.mini.mybatismini.config.datasource;

import org.h2.jdbcx.JdbcDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author nicky
 * @date 2021/9/7 上午10:54
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class H2DataSourceConfig {

    private DataSourceProperties properties;

    public H2DataSourceConfig(DataSourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DataSource dataSource() {
        JdbcDataSource jdbcDataSource = new JdbcDataSource();
        jdbcDataSource.setURL(properties.getUrl());
        jdbcDataSource.setUser(properties.getUsername());
        jdbcDataSource.setPassword(properties.getPassword());
        return jdbcDataSource;
    }
}
